package com.xc.erp.modules.erp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.xc.erp.modules.erp.domain.pojo.JshDepotItem;
import com.xc.erp.modules.erp.domain.pojo.JshMaterialCurrentStock;
import com.xc.erp.modules.erp.domain.pojo.JshSupplier;
import com.xc.erp.modules.erp.domain.vo.FindDepotVo;
import com.xc.erp.modules.erp.mapper.JshDepotItemMapper;
import com.xc.erp.modules.erp.mapper.JshMaterialCurrentStockMapper;
import com.xc.erp.modules.erp.mapper.JshSupplierMapper;
import com.xc.erp.modules.erp.service.JshDepotItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.w3c.dom.ls.LSInput;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author Administrator
 * @description 针对表【jsh_depot_item(单据子表)】的数据库操作Service实现
 * @createDate 2022-12-10 10:05:42
 */
@Service
public class JshDepotItemServiceImpl extends ServiceImpl<JshDepotItemMapper, JshDepotItem>
        implements JshDepotItemService {
    @Autowired
    private JshDepotItemMapper jshDepotItemMapper;
    @Autowired
    private JshMaterialCurrentStockMapper jshMaterialCurrentStockMapper;
    @Autowired
    private JshSupplierMapper jshSupplierMapper;

    @Override
    public List<FindDepotVo> findByDepotHeadId(Long headerId) {
        List<FindDepotVo> list = jshDepotItemMapper.findByDepotHeadId(headerId);
        if (list.get(0) == null)
            return null;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getOrganId() != null)
                list.get(i).setSupplier(jshSupplierMapper.selectOne(new QueryWrapper<JshSupplier>().eq("id", list.get(i).getOrganId())).getSupplier());
            else
                list.get(i).setSupplier(null);
            JshMaterialCurrentStock currentNumber = jshMaterialCurrentStockMapper.selectOne(new QueryWrapper<JshMaterialCurrentStock>().eq("material_id", list.get(i).getMaterialId()).eq("depot_id", list.get(i).getDepotId()));
            if (currentNumber != null)
                list.get(i).setStock(currentNumber.getCurrentNumber());
            else
                list.get(i).setStock(new BigDecimal(0));
        }

        return list;
    }
}




